﻿using DAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Aop.Api;
using Aop.Api.Domain;
using Aop.Api.Request;
using Aop.Api.Response;
using DAL;
using Permission.Web.Models;

namespace DianShang.Controllers
{
    public class ShopDetailController : Controller
    {
        // GET: ShopXiangQing
        public ActionResult Index(int id)
        {
            if (Session["login"] == null)
            {
                return Redirect("~/Home/Login");
            }
            else
            {

                ViewBag.guige = DetailServices.GetGuiGe(id);
                ViewBag.guigezhi = DetailServices.GetGuiGeZhis(id);
                DAL.Product product = DetailServices.GetProduct(id);
                return View(product);
            }
        }
        /// <summary>
        /// 提交订单页面
        /// </summary>
        /// <param name="zhi"></param>
        /// <param name="zhiid"></param>
        /// <returns></returns>
    
        public ActionResult SubmintOrder(int id,string sum) {
            string cusname = ((Customer)Session["login"]).CustomerName;
                DanPin dp = DetailServices.GetDanPin(id);
            ViewBag.Address = DetailServices.GetAddress(cusname);
            ViewBag.sum = sum;
                return View(dp);
           
           
        }
        /// <summary>
        /// 提交订单
        /// </summary>
        /// <param name="OrderBianHao"></param>
        /// <param name="Address"></param>
        /// <param name="CustomerId"></param>
        /// <param name="Phone"></param>
        /// <param name="ShopPrace"></param>
        /// <param name="ShopNum"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult SubmintOrder(string OrderBianHao ,string Address,int CustomerId,string Phone,decimal ShopPrace,int ShopNum,string name)
        {

            try
            {
                using (ShopContext db = new ShopContext())
                {
                    Order order = new Order
                    {
                        OrderBianHao = OrderBianHao,
                        CreateTime = DateTime.Now,
                        Address = Address,
                        CustomerId = Convert.ToInt32(CustomerId),
                        Phone = Phone,
                        FaHuoZhuangTai = "未发货",
                        ZhiFuZhuangTai = "未支付",
                        KeHuName = name


                    };
                    OrderDetl orderDetl = new OrderDetl
                    {
                        OrderId = Convert.ToInt32(OrderBianHao),
                        ShopId = CustomerId,
                        ShopNum = ShopNum,
                        ShopPrace = ShopPrace

                    };
                    db.Order.Add(order);
                    db.OrderDetl.Add(orderDetl);
                    int r = db.SaveChanges();
                    if (r > 0)
                    {
                        return Json(new UIResult(true, "提交订单成功"));
                    }
                    else
                    {
                        return Json(new UIResult(false, "提交订单失败"));
                    }
                }
            }
            catch (Exception ex)
            {

                return Json(new UIResult(false ,ex.Message));
            }
            
           
        }
        /// <summary>
        /// 支付
        /// </summary>
        /// <param name="pricee"></param>
        /// <param name="orderbianhao"></param>
        /// <param name="total"></param>
        /// <returns></returns>
        public ActionResult ZhiFu(decimal pricee,string orderbianhao,string total)
        {
           
                DefaultAopClient client = new DefaultAopClient(Config.gatewayUrl, Config.app_id, Config.private_key, "json", "1.0", Config.sign_type, Config.alipay_public_key, Config.charset, false);
                decimal price = pricee;
                // 外部订单号，商户网站订单系统中唯一的订单号
                string out_trade_no = orderbianhao;

                // 订单名称
                string subject = "乐乐商城购买物品";

                // 付款金额
                string total_amout = total;

                // 商品描述
                string body = "乐乐商城买的好东西";

                // 组装业务参数model
                AlipayTradePagePayModel model = new AlipayTradePagePayModel();
                model.Body = body;
                model.Subject = subject;
                model.TotalAmount = total_amout;
                model.OutTradeNo = out_trade_no;
                model.ProductCode = "FAST_INSTANT_TRADE_PAY";

                AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
            // 设置同步回调地址
           // request.SetReturnUrl("http://localhost:60380/ShopDetail/OrderUpdateState?id=" + out_trade_no);
            request.SetReturnUrl("http://localhost:60380/Home/Index");
            // 设置异步通知接收地址
            request.SetNotifyUrl("");
                // 将业务model载入到request
                request.SetBizModel(model);

                AlipayTradePagePayResponse response = null;
                try
                {
                    response = client.pageExecute(request, null, "post");

                    return Content(response.Body);
                }
                catch (Exception exp)
                {
                    throw exp;
                }


            
        }
        //public ActionResult OrderUpdateState(string id)
        //{
        //    using(ShopContext db=new ShopContext())
        //    {
        //        Order order = db.Order.SingleOrDefault(p => p.OrderBianHao == id);
        //        if (order != null)
        //        {
        //            order.ZhiFuZhuangTai = "已支付";
        //         int zt=   db.SaveChanges();
        //            if (zt > 0)
        //            {
        //                return Redirect("http://localhost:60380/Home/Index");
        //            }
        //            else
        //            {
        //                return Redirect("http://localhost:60380/Home/Index");
        //            }
        //        }
        //        else
        //        {
        //            return Redirect("http://localhost:60380/Home/Index");
        //        }
        //    }
        //}
    }
}